package com.junzhiit.nengji.cms.service.impl;

import java.sql.Time;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.junzhiit.nengji.cms.service.TableTypeService;
import com.junzhiit.nengji.cms.web.exception.ShopNotFoundException;
import com.junzhiit.nengji.cms.web.exception.TableTypeIsExistException;
import com.junzhiit.nengji.cms.web.exception.TableTypeNotFoundException;
import com.junzhiit.nengji.cms.web.util.ViewObjectUtil;
import com.junzhiit.nengji.cms.web.vo.DataTablesParamVO;
import com.junzhiit.nengji.cms.web.vo.ResultVO;
import com.junzhiit.nengji.cms.web.vo.SystemUserSessionVO;
import com.junzhiit.nengji.cms.web.vo.TableTypeAddVO;
import com.junzhiit.nengji.cms.web.vo.TableTypeEditVO;
import com.junzhiit.nengji.cms.web.vo.TableTypeVO;
import com.junzhiit.nengji.dao.ShopDAO;
import com.junzhiit.nengji.dao.TableTypeDAO;
import com.junzhiit.nengji.dao.cache.SystemUserCacheDAO;
import com.junzhiit.nengji.dao.util.DIYKey;
import com.junzhiit.nengji.model.Shop;
import com.junzhiit.nengji.model.TableType;

@Service
public class TableTypeServiceImpl extends BaseServiceImpl implements TableTypeService{
	
	@Autowired
	private SystemUserCacheDAO systemUserCacheDAO;

	@Autowired
	private ShopDAO shopDAO;
	
	@Autowired
	private TableTypeDAO tableTypeDAO;
	
	@Override
	public Map<String, Object> findPageAll(DataTablesParamVO dtp,TableTypeVO tableTypeVO) {
		SystemUserSessionVO systemUserSessionVO = systemUserCacheDAO
				.getHash(DIYKey.getSessionHashKey(tableTypeVO.getSessionId()), SystemUserSessionVO.class);
		Shop shop = shopDAO.query(systemUserSessionVO.getSystemId());
		if (shop == null) {
			throw new ShopNotFoundException("店铺不存在");
		}

		TableType tableType=new TableType();
		BeanUtils.copyProperties(tableTypeVO, tableType);
		tableType.setShopId(shop.getId());
		
		List<TableType> list = tableTypeDAO.findPageAll(tableType, dtp.getStart(), dtp.getLength());
		int totalCount = tableTypeDAO.getTotalCount(tableType);
		return ViewObjectUtil.dataTables(list, dtp.getDraw(), totalCount);
	}
	
	
	@Override
	public ResultVO findById(String id){
		ResultVO result=new ResultVO();
		
		TableType data=tableTypeDAO.query(id);
		if(data==null){
			throw new TableTypeNotFoundException("桌号类型未找到");
		}
		
		result.setData(data);
		
		return result;
	}
	
	@Override
	public ResultVO getAll(TableTypeVO tableTypeVO){
		ResultVO result=new ResultVO();
		
		SystemUserSessionVO systemUserSessionVO = systemUserCacheDAO
				.getHash(DIYKey.getSessionHashKey(tableTypeVO.getSessionId()), SystemUserSessionVO.class);
		Shop shop = shopDAO.query(systemUserSessionVO.getSystemId());
		if (shop == null) {
			throw new ShopNotFoundException("店铺不存在");
		}
		
		TableType tableType=new TableType();
		BeanUtils.copyProperties(tableTypeVO, tableType);
		tableType.setShopId(shop.getId());
		
		List<TableType> data=tableTypeDAO.getAll(tableType);
		
		
		result.setData(data);
		return result;
	}
	
	@Override
	public ResultVO add(TableTypeAddVO tableTypeAddVO){
		ResultVO result=new ResultVO();
		
		SystemUserSessionVO systemUserSessionVO = systemUserCacheDAO
				.getHash(DIYKey.getSessionHashKey(tableTypeAddVO.getSessionId()), SystemUserSessionVO.class);
		Shop shop = shopDAO.query(systemUserSessionVO.getSystemId());
		if (shop == null) {
			throw new ShopNotFoundException("店铺不存在");
		}
		
		
		TableType tableType=new TableType();
		BeanUtils.copyProperties(tableTypeAddVO,tableType);
		tableType.setEndTime(new Time(tableTypeAddVO.getEndTime().getTime()));
		tableType.setStartTime(new Time(tableTypeAddVO.getStartTime().getTime()));
		tableType.setShopId(shop.getId());
		tableType.setValidTime(tableTypeAddVO.getValidTime()*60L);
		
		int count=tableTypeDAO.getTotalCount(tableType);
		if(count>0){
			throw new TableTypeIsExistException("桌号类型已经存在");
		}
		
		TableType data=tableTypeDAO.insert(tableType);
		
		result.setData(data);
		return result;
	} 
	
	@Override
	public ResultVO edit(String id,TableTypeEditVO tableTypeEditVO){
		ResultVO result=new ResultVO();
		
		TableType data=tableTypeDAO.query(id);
		if(data==null){
			throw new TableTypeNotFoundException("桌号类型未找到");
		}
		
		TableType tableType=new TableType();
		BeanUtils.copyProperties(tableTypeEditVO,tableType);
		tableType.setEndTime(new Time(tableTypeEditVO.getEndTime().getTime()));
		tableType.setStartTime(new Time(tableTypeEditVO.getStartTime().getTime()));
		tableType.setId(id);
		tableType.setValidTime(tableTypeEditVO.getValidTime()*60L);
		
		if(!data.getName().equals(tableType.getName())){
			int count=tableTypeDAO.getTotalCount(tableType);
			if(count>0){
				throw new TableTypeIsExistException("桌号类型已经存在");
			}
		}
		
		tableTypeDAO.update(tableType);
		
		return result;
	}
	
	
	@Override
	public ResultVO updateToDelete(String id){
		ResultVO result=new ResultVO();
		
		TableType data=tableTypeDAO.query(id);
		if(data==null){
			throw new TableTypeNotFoundException("桌号类型未找到");
		}
		
		tableTypeDAO.updateToDelete(id);
		
		return result;
	}
	
}
